using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TreeNode : MonoBehaviour
{
    public int id;
    public float childSize;
    public float disChilds;
    public TreeNode leftNode;
    public TreeNode rightNode;
    public int value;
    private LineRenderer line;
    // Start is called before the first frame update
    void Start()
    {
    }
    public void Init()
    {
        var len = transform.childCount;
        line = GetComponent<LineRenderer>();
        line.positionCount = 0;
        line.material.color = new Color(Random.Range(0, 1.0f), Random.Range(0, 1.0f), Random.Range(0, 1.0f));
        line.positionCount = len * 2;
        if (len > 0)
        {
            var c1 = transform.GetChild(0);
            c1.gameObject.SetActive(leftNode != null);
            c1.localScale = Vector3.one * childSize;
            c1.localPosition = new Vector3(-disChilds, -150, 0);

        }
        if (len > 1)
        {
            var c2 = transform.GetChild(1);
            c2.gameObject.SetActive(rightNode != null);
            c2.localScale = Vector3.one * childSize;
            c2.localPosition = new Vector3(disChilds, -150, 0);

        }
        GetComponent<Text>().text = value + "";
    }

    public void LineLeft()
    {
        if (line == null) return;
        if (line.positionCount < 2) return;
        line.SetPosition(0, Vector3.zero);
        var dir = leftNode.transform.localPosition;
        dir.z -= 10;
        line.SetPosition(1, dir);
    }

    public void LineRight()
    {
        if (line == null) return;

        if (line.positionCount < 4) return;
        line.SetPosition(2, Vector3.zero);
        var dir = rightNode.transform.localPosition;
        dir.z = -1;
        line.SetPosition(3, dir);
    }
    public void ClearLine()
    {
        if (line == null) return;
        line.positionCount = 0;
        var len = transform.childCount;
        line.positionCount = len * 2;
    }
}
